package com.goldmantis.alitrip.controller;

import com.goldmantis.alitrip.service.CostCenterService;
import com.goldmantis.alitrip.service.InvoiceService;
import com.taobao.api.request.AlitripBtipCostCenterQueryRequest;
import com.taobao.api.request.AlitripBtripCostCenterModifyRequest;
import com.taobao.api.request.AlitripBtripCostCenterNewRequest;
import com.taobao.api.response.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @Author:Shmily
 * @Description:
 * @Date:Create in 17:42 2018/1/15
 */
@Controller
@RequestMapping("cost/center/")
@CrossOrigin(value = "*",maxAge = 3600)
public class CostCenterController {

    @Autowired
    private CostCenterService costCenterService;

    /**
     * 获取用户可用的成本中心
     * @param userId 用户Id(dingdingId)
     * @return
     */
    @RequestMapping(value = "getCostCenterByUserId",method = RequestMethod.GET)
    public ResponseEntity<AlitripBtripCostCenterGetResponse> getCostCenterByUserId(@RequestParam("userId") String userId) {
        try {
            AlitripBtripCostCenterGetResponse rsp = costCenterService.getCostCenterByUserId(userId);
            return ResponseEntity.ok(rsp);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    /**
     * 获取成本中心
     * @param openCostCenterQueryRq
     * @return
     */
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public ResponseEntity<AlitripBtipCostCenterQueryResponse> getCostCenter(AlitripBtipCostCenterQueryRequest.OpenCostCenterQueryRq openCostCenterQueryRq) {
        try {
            AlitripBtipCostCenterQueryResponse rsp = costCenterService.getCostCenterByQuery(openCostCenterQueryRq);
            return ResponseEntity.ok(rsp);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    /**
     * 创建成本中心
     * @param openCostCenterSaveRq
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<AlitripBtripCostCenterNewResponse> postCostCenter(@RequestBody AlitripBtripCostCenterNewRequest.OpenCostCenterSaveRq openCostCenterSaveRq) {
        try {
            AlitripBtripCostCenterNewResponse rsp = costCenterService.insert(openCostCenterSaveRq);
            return ResponseEntity.ok(rsp);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    /**
     * 修改成本中心
     * @param openCostCenterModifyRq
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT)
    public ResponseEntity<AlitripBtripCostCenterModifyResponse> putCostCenter(@RequestBody AlitripBtripCostCenterModifyRequest.OpenCostCenterModifyRq openCostCenterModifyRq) {
        try {
            AlitripBtripCostCenterModifyResponse rsp = costCenterService.update(openCostCenterModifyRq);
            return ResponseEntity.ok(rsp);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    /**
     *
     * @param thirdpart_id 第三方成本中心id
     * @return
     */
    @RequestMapping(value = "{thirdpart_id}",method = RequestMethod.DELETE)
    public ResponseEntity<AlitripBtripCostCenterDeleteResponse> deleteCostCenter(@PathVariable String thirdpart_id) {
        try {
            AlitripBtripCostCenterDeleteResponse rsp = costCenterService.delete(thirdpart_id);
            return ResponseEntity.ok(rsp);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

}
